<template>

  <basic-container>

    <avue-crud
        v-if="tableOption"
        ref="avueCrud"
        v-bind="bindVal"
        v-on="onEvent"
    >
    </avue-crud>
  </basic-container>

</template>

<script>

import {createStaticMixin} from "@/mixins/startCrud.js"
import tagTools from "components/avue/utils/tagTools";
import dictionaryItemOption from "@/option/sys/dictionaryItemOption"
import * as dictionaryItemApi from "@/api/sys/dictionaryItemApi"

const crudOption = {}
crudOption.option = dictionaryItemOption
crudOption.api = dictionaryItemApi
crudOption.permissionBtnHandler = {
  delBtn: (vm, row, defaultValue) => {
    return !row.sysFlag;
  }
}
export default {
  name: "item",
  mixins: [createStaticMixin(crudOption)],
  data() {
    return {
      dialogVisible: false,
      dictCode: null,
      isLoading: false,
    }
  },
  created() {
    this.dictCode = this.$route.params.dictCode
    this.searchParams.start_search_dictCode = this.dictCode;
  },
  methods: {
    loadCrudColumnBefore(column) {
      if ('parentId' !== column.prop) {
        return
      }
      column.dicQuery.dictCode = this.dictCode;
    },
    beforeLoadDataSource(searchParams) {
      searchParams.start_search_dictCode = this.dictCode
    },

    beforeOpenEditDialog(row, done) {
      if (!row.tagProp) {
        row.tagProp = {}
      }
      let tagProp = row.tagProp || {}
      let keys = Object.values(tagTools.tagDicProps)
      for (let i = 0; i < keys.length; i++) {
        let key = keys[i];
        row["tagProp." + key] = tagProp[key];
      }
      done();
    },

    beforeRowUpdate(row) {
      this.convertTagPropToObj(row)
    },
    rowSaveBefore(row, index) {
      row.dictCode = this.dictCode;
      this.convertTagPropToObj(row)
    },
    convertTagPropToObj(row) {
      let tagProp = row.tagDicProps || {}
      let keys = Object.values(tagTools.tagDicProps)
      for (let i = 0; i < keys.length; i++) {
        let key = keys[i];
        let srcKey = "tagProp." + key
        tagProp[key] = row[srcKey];
        delete row[srcKey]
      }
      row.tagProp = tagProp
    },


  }
}
</script>

<style scoped>

</style>
